////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2008 Michiel Helvensteijn                                    //
//                                                                            //
// This file is part of the Mist compiler.                                    //
//                                                                            //
// The Mist compiler is free software: you can redistribute it and/or modify  //
// it under the terms of the GNU General Public License as published by       //
// the Free Software Foundation, either version 3 of the License, or          //
// (at your option) any later version.                                        //
//                                                                            //
// The Mist compiler is distributed in the hope that it will be useful,       //
// but WITHOUT ANY WARRANTY; without even the implied warranty of             //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              //
// GNU General Public License for more details.                               //
//                                                                            //
// You should have received a copy of the GNU General Public License          //
// along with the Mist compiler.  If not, see <http://www.gnu.org/licenses/>. //
////////////////////////////////////////////////////////////////////////////////

/******************************************************************************
 * Includes                                                                   *
 ******************************************************************************/

#include <vector>
using std::vector;

#include <string>
using std::string;

#include "common.hpp"

/******************************************************************************
 * Test Cases                                                                 *
 ******************************************************************************/

BOOST_AUTO_TEST_SUITE( suite_common )
	
	BOOST_AUTO_TEST_CASE( test_to_string ) {
		BOOST_CHECK_EQUAL( to_string(100  ), "100"    );
		BOOST_CHECK_EQUAL( to_string(-50  ), "-50"    );
		BOOST_CHECK_EQUAL( to_string(3e5  ), "300000" );
		BOOST_CHECK_EQUAL( to_string(0.1  ), "0.1"    );
		BOOST_CHECK_EQUAL( to_string(-0.1 ), "-0.1"   );
		BOOST_CHECK_EQUAL( to_string(0.1e5), "10000"  );
		BOOST_CHECK_EQUAL( to_string("foo"), "foo"    );
	}
	
	BOOST_AUTO_TEST_CASE( test_is_prefix ) {
		vector<int> needle, haystack, eldeen;
		
		needle   += 1,2,3,4,5;
		haystack += 1,2,3,4,5,6,7,8,9;
		eldeen   += 5,4,3,2,1;
		
		BOOST_CHECK_PREDICATE( is_prefix<int>     , (needle)(haystack) );
		BOOST_CHECK_PREDICATE( is_prefix<int>     , (needle)(needle)   );
		BOOST_CHECK_PREDICATE( neg(is_prefix<int>), (haystack)(needle) );
		BOOST_CHECK_PREDICATE( neg(is_prefix<int>), (eldeen)(needle)   );
		BOOST_CHECK_PREDICATE( neg(is_prefix<int>), (needle)(eldeen)   );
	}
	
	BOOST_AUTO_TEST_CASE( test_max ) {
		BOOST_CHECK_EQUAL( max( 1,    2  ),  2   );
		BOOST_CHECK_EQUAL( max(-1,   -2  ), -1   );
		BOOST_CHECK_EQUAL( max( 0.1,  0.2),  0.2 );
		BOOST_CHECK_EQUAL( max(-0.1, -0.2), -0.1 );
	}
	
	BOOST_AUTO_TEST_CASE( test_digit_count ) {
		BOOST_CHECK_EQUAL( digit_count(1)          , 1 );
		BOOST_CHECK_EQUAL( digit_count(25)         , 2 );
		BOOST_CHECK_EQUAL( digit_count(931)        , 3 );
		BOOST_CHECK_EQUAL( digit_count(1234)       , 4 );
		BOOST_CHECK_EQUAL( digit_count(1234567890l), 10);
		
		BOOST_CHECK_THROW( digit_count(-5), AssertionFailure );
	}
	
	BOOST_AUTO_TEST_CASE( test_to ) {
		BOOST_CHECK_EQUAL( to<int>   ("100"  ),  100     );
		BOOST_CHECK_EQUAL( to<int>   ("-50"  ), -50      );
		BOOST_CHECK_EQUAL( to<double>("0.1"  ),  0.1     );
		BOOST_CHECK_EQUAL( to<double>("-0.1" ), -0.1     );
		BOOST_CHECK_EQUAL( to<double>("0.1e5"),  10000.0 );
		BOOST_CHECK_EQUAL( to<double>("100")  ,  100     );
		
		BOOST_CHECK_THROW( to<int>   ("3e5"  ), AssertionFailure ); // no scientific notation for int
		BOOST_CHECK_THROW( to<int>   ("5.4"  ), AssertionFailure );
		BOOST_CHECK_THROW( to<int>   ("text" ), AssertionFailure );
		BOOST_CHECK_THROW( to<double>("text" ), AssertionFailure );
	}
	
	BOOST_AUTO_TEST_CASE( test_is ) {
		BOOST_CHECK_PREDICATE( is<int>        , ("100"  ) );
		BOOST_CHECK_PREDICATE( is<int>        , ("-50"  ) );
		BOOST_CHECK_PREDICATE( is<double>     , ("0.1"  ) );
		BOOST_CHECK_PREDICATE( is<double>     , ("-0.1" ) );
		BOOST_CHECK_PREDICATE( is<double>     , ("0.1e5") );
		BOOST_CHECK_PREDICATE( is<double>     , ("100"  ) );
		
		BOOST_CHECK_PREDICATE( neg(is<int>)   , ("3e5"  ) ); // no scientific notation for int
		BOOST_CHECK_PREDICATE( neg(is<int>)   , ("5.4"  ) );
		BOOST_CHECK_PREDICATE( neg(is<int>)   , ("text" ) );
		BOOST_CHECK_PREDICATE( neg(is<double>), ("text" ) );
	}
	
	BOOST_AUTO_TEST_CASE( test_implode ) {
		vector<string> s;
		s += "1","2","3","4","5";
		
		BOOST_CHECK_EQUAL( implode(s, ","          ), "1,2,3,4,5"        );
		BOOST_CHECK_EQUAL( implode(s, ", ", " and "), "1, 2, 3, 4 and 5" );
		
		vector<int> v;
		v += 1,2,3,4,5;
		
		BOOST_CHECK_EQUAL( implode(v, ","          ), "1,2,3,4,5"        );
		BOOST_CHECK_EQUAL( implode(v, ", ", " and "), "1, 2, 3, 4 and 5" );
	}
	
	BOOST_AUTO_TEST_CASE( test_ordinal_indicator ) {
		BOOST_CHECK_EQUAL( ordinal_indicator( 1), "st" );
		BOOST_CHECK_EQUAL( ordinal_indicator( 2), "nd" );
		BOOST_CHECK_EQUAL( ordinal_indicator( 3), "rd" );
		BOOST_CHECK_EQUAL( ordinal_indicator( 4), "th" );
		BOOST_CHECK_EQUAL( ordinal_indicator( 5), "th" );
		
		BOOST_CHECK_EQUAL( ordinal_indicator(10), "th" );
		BOOST_CHECK_EQUAL( ordinal_indicator(11), "th" );
		BOOST_CHECK_EQUAL( ordinal_indicator(12), "th" );
		BOOST_CHECK_EQUAL( ordinal_indicator(13), "th" );
		BOOST_CHECK_EQUAL( ordinal_indicator(14), "th" );
		BOOST_CHECK_EQUAL( ordinal_indicator(15), "th" );
		
		BOOST_CHECK_EQUAL( ordinal_indicator(20), "th" );
		BOOST_CHECK_EQUAL( ordinal_indicator(21), "st" );
		BOOST_CHECK_EQUAL( ordinal_indicator(22), "nd" );
		BOOST_CHECK_EQUAL( ordinal_indicator(23), "rd" );
		BOOST_CHECK_EQUAL( ordinal_indicator(24), "th" );
		BOOST_CHECK_EQUAL( ordinal_indicator(25), "th" );
	}
	
	BOOST_AUTO_TEST_CASE( test_ordinal ) {
		BOOST_CHECK_EQUAL( ordinal( 1), "1st" );
		BOOST_CHECK_EQUAL( ordinal( 2), "2nd" );
		BOOST_CHECK_EQUAL( ordinal( 3), "3rd" );
		BOOST_CHECK_EQUAL( ordinal( 4), "4th" );
		BOOST_CHECK_EQUAL( ordinal( 5), "5th" );
		
		BOOST_CHECK_EQUAL( ordinal(10), "10th" );
		BOOST_CHECK_EQUAL( ordinal(11), "11th" );
		BOOST_CHECK_EQUAL( ordinal(12), "12th" );
		BOOST_CHECK_EQUAL( ordinal(13), "13th" );
		BOOST_CHECK_EQUAL( ordinal(14), "14th" );
		BOOST_CHECK_EQUAL( ordinal(15), "15th" );
		
		BOOST_CHECK_EQUAL( ordinal(20), "20th" );
		BOOST_CHECK_EQUAL( ordinal(21), "21st" );
		BOOST_CHECK_EQUAL( ordinal(22), "22nd" );
		BOOST_CHECK_EQUAL( ordinal(23), "23rd" );
		BOOST_CHECK_EQUAL( ordinal(24), "24th" );
		BOOST_CHECK_EQUAL( ordinal(25), "25th" );
	}
	
BOOST_AUTO_TEST_SUITE_END()
